#!/bin/bash

# 环境变量配置文件，用于存储不同环境的配置

# 开发环境配置
dev_config() {
    export DEPLOY_ENV="dev"
    export MYSQL_ROOT_PASSWORD="123456"
    export MYSQL_USER="root"
    export MYSQL_PASSWORD="123456"
    export MYSQL_DATABASE="payservice"
    export MYSQL_PORT="3306"
    export REDIS_PASSWORD=""
    export REDIS_PORT="6379"
    export JAVA_OPTS="-Xms512m -Xmx1g"
    export DATA_CENTER_ID="1"
    export WORKER_ID="1"
    export APP_PORT="8080"
}

# 测试环境配置
test_config() {
    export DEPLOY_ENV="test"
    export MYSQL_ROOT_PASSWORD="test_password"
    export MYSQL_USER="test_user"
    export MYSQL_PASSWORD="test_password"
    export MYSQL_DATABASE="payservice"
    export MYSQL_PORT="3306"
    export REDIS_PASSWORD=""
    export REDIS_PORT="6379"
    export JAVA_OPTS="-Xms1g -Xmx2g"
    export DATA_CENTER_ID="1"
    export WORKER_ID="1"
    export APP_PORT="8080"
}

# UAT环境配置
uat_config() {
    export DEPLOY_ENV="uat"
    export MYSQL_ROOT_PASSWORD="uat_password"
    export MYSQL_USER="uat_user"
    export MYSQL_PASSWORD="uat_password"
    export MYSQL_DATABASE="payservice"
    export MYSQL_PORT="3306"
    export REDIS_PASSWORD="uat_redis_password"
    export REDIS_PORT="6379"
    export JAVA_OPTS="-Xms2g -Xmx4g"
    export DATA_CENTER_ID="2"
    export WORKER_ID="1"
    export APP_PORT="8080"
}

# 生产环境配置
prod_config() {
    export DEPLOY_ENV="prod"
    export MYSQL_ROOT_PASSWORD="prod_password"
    export MYSQL_USER="prod_user"
    export MYSQL_PASSWORD="prod_password"
    export MYSQL_DATABASE="payservice"
    export MYSQL_PORT="3306"
    export REDIS_PASSWORD="prod_redis_password"
    export REDIS_PORT="6379"
    export JAVA_OPTS="-Xms4g -Xmx8g -XX:+UseG1GC -XX:MaxGCPauseMillis=200"
    export DATA_CENTER_ID="1"
    export WORKER_ID="1"
    export APP_PORT="8080"
}

# 根据参数加载对应环境的配置
load_config() {
    local env=$1
    case $env in
        dev)
            dev_config
            ;;
        test)
            test_config
            ;;
        uat)
            uat_config
            ;;
        prod)
            prod_config
            ;;
        *)
            echo "错误: 不支持的环境 '$env'"
            echo "支持的环境: dev, test, uat, prod"
            return 1
            ;;
    esac
    
    echo "已加载 $env 环境配置"
    return 0
}

# 如果直接执行此脚本，则显示帮助信息
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
    echo "环境变量配置文件"
    echo "用法: source $(basename ${BASH_SOURCE[0]}) ENV"
    echo "示例: source $(basename ${BASH_SOURCE[0]}) dev"
    echo ""
    echo "支持的环境:"
    echo "  dev  - 开发环境"
    echo "  test - 测试环境"
    echo "  uat  - 用户验收测试环境"
    echo "  prod - 生产环境"
    
    # 如果提供了参数，则尝试加载配置
    if [[ $# -gt 0 ]]; then
        load_config "$1"
    fi
fi 